package com.open.rbac.config.executor;

import java.util.concurrent.ThreadPoolExecutor;

import lombok.Data;

/**
 * 线程池信息
 *
 * @author Riche's
 * @since 2025/4/29
 */
@Data
public class TaskExecutorInfo {

    /**
     * 线程池名称
     */
    private String poolName;

    /**
     * 线程名称前缀
     */
    private String threadNamePrefix;

    /**
     * 线程池中允许的最大线程数，线程池中的当前线程数目不会超过该值。
     * 如果队列中任务已满，并且当前线程个数小于maximumPoolSize，那么会创建新的线程来执行任务
     */
    private long maxPoolSize;

    /**
     * 线程池从创建到现在，池中线程的最大数量
     * 即曾经达到的最大值 maxPoolSize >= largestPoolSize
     */
    private long largestPoolSize;

    /**
     * 线程池的基本大小，即在没有任务需要执行的时候线程池的大小，并且只有在工作队列满了的情况下才会创建超出这个数量的线程
     */
    private long corePoolSize;

    /**
     * 线程池中当前线程的数量，当该值为0的时候，意味着没有任何线程，线程池会终止；同一时刻，poolSize不会超过maximumPoolSize。
     */
    private long poolSize;

    /**
     * 线程池总任务数
     */
    private long taskCount;

    /**
     * 线程池已完成的任务数
     */
    private long completedTaskCount;

    /**
     * 活跃的任务数
     */
    private long activeCount;

    /**
     * 拒绝任务数
     */
    private long rejectedCount;

    /**
     * 缓冲队列大小
     */
    private long queueSize;

    /**
     * 队列剩余容量
     */
    private int queueRemainingCapacity;

    /**
     * 线程池状态
     */
    private boolean isShutdown;

    /**
     * 线程池是否终止
     */
    private boolean isTerminated;


    /**
     * 构造方法
     *
     * @param taskExecutor 线程池
     */
    public TaskExecutorInfo(OpenTaskExecutor taskExecutor) {
        super();
        ThreadPoolExecutor executor = taskExecutor.getThreadPoolExecutor();
        this.poolName = taskExecutor.getPoolName();
        this.threadNamePrefix = taskExecutor.getThreadNamePrefix();
        this.maxPoolSize = executor.getMaximumPoolSize();
        this.largestPoolSize = executor.getLargestPoolSize();
        this.corePoolSize = executor.getCorePoolSize();
        this.poolSize = executor.getPoolSize();
        this.taskCount = executor.getTaskCount();
        this.completedTaskCount = executor.getCompletedTaskCount();
        this.activeCount = executor.getActiveCount();
        this.queueSize = executor.getQueue().size();
        this.queueRemainingCapacity = executor.getQueue().remainingCapacity();
        this.rejectedCount = taskExecutor.getRejectedCount().get();
        this.isShutdown = executor.isShutdown();
        this.isTerminated = executor.isTerminated();
    }

}
